package main.java.cn.wolfcode.service.impl;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void insert(Employee employee, Long[] ids) {
        //对要保存的密码进行加密
        Md5Hash md5Hash = new Md5Hash(employee.getPassword(),employee.getUsername());
        employee.setPassword(md5Hash.toString());
        employeeMapper.insert(employee);
        if (ids != null && ids.length > 0) {
            for (Long rid : ids) {
                //处理中间表
                employeeMapper.insertRelation(employee.getId(), rid);
            }
        }


    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRelation(id);
    }

    @Override
    public void updateByPrimaryKey(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //删除关系
        employeeMapper.deleteRelation(employee.getId());
        //关联关系
        //处理中间表(关联关系)
        if (ids != null && ids.length > 0) {
            for (Long rid : ids) {
                employeeMapper.insertRelation(employee.getId(), rid);
            }
        }
    }

    @Override
    public Employee selectByPrimaryKey(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public List<Employee> selectAll() {
        List<Employee> list = employeeMapper.selectAll();
        return list;
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());

        List<Employee> employees = employeeMapper.queryForList(qo);
        return new PageInfo<Employee>(employees);

    }

    @Override
    public Employee login(String username, String password) {
        if(!StringUtils.hasText(username)){
            throw new RuntimeException("用户名不能为空");
        }
        if(!StringUtils.hasText(password)){
            throw new RuntimeException("密码不能为空");
        }
        Employee employee = employeeMapper.selectByUsernameAndPassword(username,password);

        if(employee==null){
            throw new RuntimeException("用户名或密码错误");
        }
        return employee;
    }

    @Override
    public Employee selectByUsername(String username) {
        if(!StringUtils.hasText(username)){
            throw  new RuntimeException("用户名不能为空");
        }

        Employee employee = employeeMapper.selecByUsername(username);
        if (employee == null) {
            throw new RuntimeException("用户名错误");
        }
        return employee;
    }

    @Override
    public Workbook exportXls() {
        //查询所有的员工数据
        List<Employee> employees = employeeMapper.selectAll();
        //创建工作簿Excel
        Workbook workbook = new HSSFWorkbook();
        //创建工作表
        Sheet sheet = workbook.createSheet("通讯录");
        //创建行,从哪一行开始
        Row row = sheet.createRow(0);
        //创建单元格
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        for (int i = 0; i < employees.size(); i++) {
            row=sheet.createRow(i+1);
            Employee employee = employees.get(i);
            //创建单元格
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            if(employee.getAge()!=null){
                row.createCell(2).setCellValue(employee.getAge());
            }
        }

        return workbook;
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        //把接受到的文件以Excel的方式去读取操作
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        //读取第一页
        HSSFSheet sheetAt = workbook.getSheetAt(0);
        //获取最后一行的索引
        int lastRowNum = sheetAt.getLastRowNum();
        //从索引为一的行数开始读(第一行是列名,最后一行也要读所以要等)
        for (int i = 1; i <=lastRowNum ; i++) {
            //获取行数据
            HSSFRow row = sheetAt.getRow(i);
            String username=row.getCell(0).getStringCellValue();
            String email=row.getCell(1).getStringCellValue();
            int age = (int) row.getCell(2).getNumericCellValue();
            Employee employee = new Employee();
            employee.setUsername(username);
            employee.setEmail(email);
            employee.setAge(age);
            employeeMapper.insert(employee);

        }
    }

    @Override
    public void updatePasswordByEmpId(Long id) {
        employeeMapper.updatePasswordByEmpId(id);
    }

    @Override
    public void updatePassword(String newPassword,Long id) {
        employeeMapper.updatePassword(newPassword,id);
    }

}
